<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 *
 * @package     Real Estate CMS Pro
 * @author      Nurul Amin Muhit
 * @email       muhit18@gmail.com
 */
class Admin_Files extends CI_Controller {

    function __construct() {
        parent::__construct();

        $this->load->database();
        $this->load->library('ion_auth');
        $this->load->library('form_validation');
        $this->load->library('session');
        $this->load->helper('pagination');
        $this->load->helper('language');

        $this->load->helper('auth');
        $this->load->model('users_m');
        $this->load->model('admin_estate_model');
        $this->load->model('settings_m');
        $this->lang->load('admin_estate');

        $this->load->model('files_m');
        $this->load->helper('language');
    }

    public function index() {
        is_login(4);
        $this->session->set_userdata('page', 0);
        $this->data['ptitle'] = $this->lang->line('files') . ' ' . $this->lang->line('dashboard');
        $this->data['files'] = $this->files_m->get_files();
        $this->data['success'] = $this->session->flashdata('success');
        $this->data['error'] = $this->session->flashdata('error');

        $this->tmadmin->tmView('files/index', $this->data);
    }

    public function view() {
        $this->session->set_userdata('page', 0);
        $this->data['ptitle'] = $this->lang->line('file:title:file_details');
        $this->data['file_details'] = $this->_get_page($this->uri->segment(4));

        if ($this->uri->segment(4) == 'add_file') {
            $loaded = 'add_file';
        } elseif ($this->uri->segment(4) == 'file') {
            $loaded = 'view_file';
        } else {
            $loaded = 'index';
        }
        $this->tmadmin->tmView('files/' . $loaded, $this->data);
    }

    public function edit() {
        is_login(4);

        if ($this->input->post()) {
            $id = $this->input->post('file_id');
            $packages = serialize($this->input->post('memberships'));

            if (!$this->input->post('private')) {
                $private = '0';
            } else {
                $private = '1';
            }

            $data = array(
                'name' => $this->input->post('name'),
                'private' => $private,
                'packages' => $packages,
                'notes' => $this->input->post('notes')
            );

            $this->db->where('file_id', $id);
            $this->db->update('files', $data);
            $this->_log_activity($this->lang->line('edited_file'));
            $this->session->set_flashdata('message', lang('record_successfully_updated'));
            redirect('admin/files/view/file/' . $id);
        } else {

            $data['ptitle'] = lang('files') . ' ' . lang('dashboard');

            $this->tmadmin->tmView('files/edit_file', $this->data);
        }
    }

    public function upload_file() {
        is_login(4);

        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'gif|jpg|png|doc|txt|pdf|docx';
        $config['max_size'] = $this->config->item('file_max_size');
        $config['encrypt_name'] = $this->config->item('encrypt_file_name');

        $this->load->library('upload', $config);

        if (!$this->upload->do_upload()) {
            $this->session->set_flashdata('message', $this->lang->line('failed_upload'));
            redirect('admin/files/view/add_file');
        } else {
            $data = $this->upload->data();
            $packages = serialize($this->input->post('memberships'));
            $file_id = $this->files_m->insert_file($data['file_name'], $_POST['file_name'], $packages, $_POST['private'], $_POST['notes']);
            $this->_log_activity($this->lang->line('added_new_file'));
            $this->session->set_flashdata('message', $this->lang->line('record_successfully_created'));
            redirect('admin/files/view/add_file');
        }
    }

    public function delete_file($file_id) {
        is_login(4);

        if ($this->files_m->delete_file($file_id)) {
            $this->_log_activity($this->lang->line('deleted_file'));
            $this->session->set_flashdata('message', $this->lang->line('record_successfully_deleted'));
            redirect('admin/files');
        } else {
            $this->session->set_flashdata('message', $this->lang->line('failed_delete'));
            redirect('admin/files');
        }
    }

    public function download($file_id) {
        $this->load->helper('download');
        if ($this->files_m->get_file($file_id)) {
            $file = $this->files_m->get_file($file_id);
            $data = file_get_contents('./uploads/' . $file->file); // Read the file's contents
            force_download($file->file, $data);
        } else {
            $this->session->set_flashdata('message', $this->lang->line('failure'));
            redirect('admin/files');
        }
    }

    function _get_page($status) {

        switch ($status) {
            case 'file':
                return $this->files_m->get_file_details($this->uri->segment(5) / 1200);
                break;
            default:
                break;
        }
    }

    function _log_activity($message) {
        $this->db->set('user', $this->session->userdata('user_id'));
        $this->db->set('message', $message);
        $this->db->insert('timeline');
        return TRUE;
    }

}

/* End of file admin_files.php */
/* Location: ./application/controllers/admin_files.php */